// pages/my/my.js
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    name: "微信用户",
    image: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    openid: "",
    orderCount: '1',
    faCount: 0,
    shouCount: 0,
    pingCount: 0,
    showFa: false,
    showShou: false,
    showPing: false,
    faList: [],
    shouList: [],
    doneList: [],
    pingList: [],
    userInfo: {
      image: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
      name: "未登录",
      openid: ""
    },
    class: "点击登录",
    otherService: [{
      name: "location",
      title: "收货地址",
      id: '1'
    }, {
      name: "question",
      title: "常见问题",
      id: '2'
    }, {
      name: "setting",
      title: "设置",
      id: '3'
    }]
  },
  //判断是否存在缓存信息
  goLogin() {
    if (!wx.getStorageSync('userInfo')) {
      this.login()
    } else {
      wx.navigateTo({
        url: '../updataUserInfo/updataUserInfo?openid=' + this.data.userInfo.openid,
      })
    }
  },
  // 登录函数
  login() {
    const that = this
    wx.cloud.callFunction({
      name: "login",
      // 获取用户openid
      success: res => {
        console.log("云函数调用成功", res.result)
        that.data.openid = res.result.openid
        wx.showModal({
          content: '确定要授权登录吗？',
          confirmColor: '#ff0000',
          success(res) {
            if (res.confirm) {
              // 获取用户openid
              that.setData({
                class: "点击修改信息",
                userInfo: {
                  image: that.data.image,
                  name: that.data.name,
                  openid: that.data.openid
                }
              })
              that.loginAfter();
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  },
  // 将用户信息以及openid存起来，方便二次进入小程序使用
  loginAfter() {
    wx.setStorageSync('userInfo', this.data.userInfo)
    this.onShow()
  },
  // 进入收藏界面
  goToCollection(e) {
    app.navigateTo('../collection/collection', true)
  },
  // 进入预定界面
  goToReservation() {
    app.navigateTo('../reservation/reservation', true)
  },
  //跳转到设置页面
  goOtherPage(e) {
    if (e.currentTarget.dataset.id == '3') {
      app.navigateTo('../setting/setting')
    } else if (e.currentTarget.dataset.id == '1') {
      app.navigateTo('../addressinfo/addressinfo', true)
    }
  },
  checkOrder(e) {
    if (wx.getStorageSync('userInfo')) {
      if (e.currentTarget.id == '3') {
        // app.navigateTo('../orderDetail/orderDetail')
        var List = JSON.stringify(this.data.pingList);
        wx.navigateTo({
          url: '../goEvaluateDetail/goEvaluateDetail?List=' + List,
        })
      } else if (e.currentTarget.id == '2') {
        // app.navigateTo('../orderDetail/orderDetail')
        var List = JSON.stringify(this.data.shouList);
        wx.navigateTo({
          url: '../orderDetail/orderDetail?List=' + List,
        })
      } else if (e.currentTarget.id == '1') {
        var List = JSON.stringify(this.data.faList);
        wx.navigateTo({
          url: '../orderDetail/orderDetail?List=' + List,
        })
      }
    } else {
      wx.showToast({
        title: "请先登录",
        icon: "none"
      })
    }
  },
  goToDoneCart() {
    var List = JSON.stringify(this.data.doneList);
    if (wx.getStorageSync('userInfo')) {
      wx.navigateTo({
        url: '../orderDetail/orderDetail?List=' + List,
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: "none"
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    if (wx.getStorageSync('userInfo')) {
      this.setData({
        class: "点击修改信息",
        userInfo: wx.getStorageSync('userInfo')
      })
    }
    this.setData({
      faCount: 0,
      shouCount: 0,
      pingCount: 0,
      showFa: false,
      showShou: false,
      showPing: false,
      faList: [],
      shouList: [],
      doneList: [],
      pingList: []
    })
    // 读取orderList中订单的数量
    let that = this
    app.getList("orderList")
      .then(res => {
        // console.log(res.data[0].state)
        // 每次读取之前都要清空现有的列表，不然值会叠加
        let faList = new Array()
        let shouList = new Array()
        let doneList = new Array()
        let pingList = new Array()
        for (let i = 0; i < res.data.length; i++) {
          if (res.data[i].state === '待发货') {
            // that.data.faCount++
            that.data.faList = faList
            faList.push(res.data[i])
          } else if (res.data[i].state === '待收货') {
            that.data.shouList = shouList
            shouList.push(res.data[i])
            console.log("aqa", that.data.shouList)
            //  console.log(that.data.shouList)
          } else if (res.data[i].state === '已收货') {
            that.data.doneList = doneList
            doneList.push(res.data[i])
            for (let j = 0; j < res.data[i].myOrder.length; j++) {
              if (res.data[i].myOrder[j].evaluate == "待评价") {
                that.data.pingList = pingList
                pingList.push(res.data[i].myOrder[j])
              }
            }
          }
        }
        if (that.data.faList.length != 0 && wx.getStorageSync('userInfo')) {
          this.setData({
            faCount: that.data.faList.length,
            showFa: true
          })
        }
        if (that.data.shouList.length != 0 && wx.getStorageSync('userInfo')) {
          this.setData({
            shouCount: that.data.shouList.length,
            showShou: true
          })
        }
        if (that.data.pingList.length != 0 && wx.getStorageSync('userInfo')) {
          this.setData({
            pingCount: that.data.pingList.length,
            showPing: true
          })
        }
      })
  }
})